const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');

const plugins = [
  new HtmlWebpackPlugin({
    template: './example/index.html', // 指定 HTML 模板
  }),
  new webpack.ProgressPlugin({
    activeModules: true, // 显示活跃的模块
    entries: true,       // 显示入口点
    modules: true,       // 显示模块
    modulesCount: 5000,  // 显示模块数量的上限
    profile: true,       // 显示性能分析
    dependencies: true,  // 显示依赖
    dependenciesCount: 10000, // 显示依赖数量的上限
  }),
  new CopyWebpackPlugin({
    patterns: [
      {
        from: 'node_modules/pdfjs-dist/build/pdf.worker.min.js',
        to: 'pdf.worker.min.js',
      },
    ],
  }),
];

const port = 3000; // 服务器端口

module.exports = {
  mode: 'development', // 开发模式
  entry: './example/main.js', // 开发环境入口文件
  output: {
    filename: 'lr-file-preview.bundle.js',
    path: path.resolve(__dirname, '../dist'),
  },
  devServer: {
    static: {
      directory: path.join(__dirname, '../example'), // 静态资源目录
    },
    compress: true, // 开启 gzip 压缩
    port, // 服务器端口
    open: true, // 自动打开浏览器
    hot: true, // 热更新
    historyApiFallback: true, // 支持前端路由
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader'],
      },
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-react'], // 确保支持 JSX
          },
        },
      },
    ],
  },
  plugins,
};